import {
  Controller,
  Get,
  Query,
  Post,
  Body,
  Put,
  Param,
  Delete,
  Res,
  HttpStatus,
  HttpException,
} from '@nestjs/common';
import {
  CreateCatDto,
  UpdateCatDto,
  ListAllEntities,
} from '../cats/dto/create-cat.dto';
import { Response } from 'express';

@Controller('cats')
export class CatsController {
  @Post()
  create(@Body() createCatDto: CreateCatDto) {
    return 'This action adds a new cat';
  }

  @Get()
  // findAll(@Query() query: ListAllEntities) {
  //   return `This action returns all cats (name: ${query.name}; age: ${query.age}; breed: ${query.breed}; limit: ${query.limit} items) `;
  // }

  // 抛出标准异常
  async findAll(@Query() query) {
    // throw new HttpException({ "message": 'Server error', "code":HttpStatus.FORBIDDEN }, HttpStatus.FORBIDDEN);
    try {
      return `This action returns all cats`
    } catch (error) { 
      throw new HttpException({
        status: HttpStatus.FORBIDDEN,
        error: 'This is a custom message',
      }, HttpStatus.FORBIDDEN, {
        cause: error
      });
    }
  }

  @Get(':id')
  // findOne(@Param('id') id: string) {
  findOne(@Param('id') id: string, @Res() res: Response) {
    // return `This action returns a #${id} cat`;
    res.status(HttpStatus.OK).json([`This action returns a #${id} cat`]);
  }

  @Put(':id')
  update(@Param('id') id: string, @Body() updateCatDto: UpdateCatDto) {
    return `This action updates a #${id} cat`;
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return `This action removes a #${id} cat`;
  }
}
